package day_2022_9_to_12.Day;

import java.util.Arrays;

/**
 * @author haomin
 * @date 2022/09/08 21:21
 **/
public class Day6_11_Num169_MajorityElement {
    class Solution1 {
        public int majorityElement(int[] nums) {
            Arrays.sort(nums);
            return nums[nums.length/2];
        }
    }
    class Solution {
        public int majorityElement(int[] nums) {
            int target = 0, count = 0;
            for(int num : nums){
                if(count == 0){
                    target = num;
                    count = 1;
                }else if(target == num) count++;
                else if(target != num) count--;
            }
            return target;
        }
    }
}